package ing.gzq.controller;

import ing.gzq.base.Result;
import ing.gzq.base.ResultCache;
import ing.gzq.model.*;
import ing.gzq.service.CourseService;
import ing.gzq.service.NoticeService;
import ing.gzq.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static ing.gzq.base.ResultCache.getFailureDetail;

/**
 * Created by gzq on 17-7-20.
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    CourseService courseService;

    @Autowired
    NoticeService noticeService;

    @Autowired
    QuestionService questionService;

    @RequestMapping(value = "/course", method = RequestMethod.POST)
    public Result createCourse(Authentication auth, Course course) {
        course.setTeacherId(((User) auth.getPrincipal()).getUsername());
        return courseService.insertCourse(course);
    }

    @RequestMapping(value = "/course/{courseId}", method = RequestMethod.POST)
    public Result modifyCourse(@PathVariable Long courseId, Course course) {
        course.setId(courseId);
        if(StringUtils.isEmpty(course.getIntroduce()))
            return getFailureDetail("introduce 为空");
        return courseService.modifyCourseIntroduce(course);
    }

    @RequestMapping(value = "/notice/{courseId}", method = RequestMethod.POST)
    public Result uploadNotice(@PathVariable Long courseId, Notice notice) {
        notice.setCourseId(courseId);
        return noticeService.uploadNotice(notice);
    }

    @RequestMapping(value = "/notice/delete/{noticeId}", method = RequestMethod.POST)
    public Result deleteNotice(@PathVariable Long noticeId) {
        return noticeService.deleteNotice(noticeId);
    }

    @RequestMapping(value = "/member/{courseId}", method = RequestMethod.GET)
    public Result getMember(@PathVariable Long courseId) {
        return courseService.getAllJoinedStudent(courseId);
    }

    @RequestMapping(value = "/member/delete/{courseId}/{studentId}", method = RequestMethod.POST)
    public Result deleteMember(@PathVariable Long courseId,@PathVariable  String studentId) {
        return courseService.deleteMember(courseId,studentId);
    }

    @RequestMapping(value = "/course/start/{courseId}", method = RequestMethod.POST)
    public Result startNewLesson(@PathVariable Long courseId) {
        return courseService.startCourse(courseId);
    }

    @RequestMapping(value = "/course/end/{courseId}", method = RequestMethod.POST)
    public Result endLesson(@PathVariable Long courseId) {
        return courseService.endCourse(courseId);
    }

    @RequestMapping(value = "/course/start/live/{courseId}", method = RequestMethod.POST)
    public Result startLive(@PathVariable Long courseId) {
        return courseService.startLive(courseId);
    }

    @RequestMapping(value = "/course/end/live/{courseId}", method = RequestMethod.POST)
    public Result endLive(@PathVariable Long courseId) {
        return courseService.endLive(courseId);
    }
    
    @RequestMapping(value = "/course/start/chat/{courseId}", method = RequestMethod.POST)
    public Result openChat(@PathVariable Long courseId) {
        return courseService.openChat(courseId);
    }

    @RequestMapping(value = "/course/end/chat/{courseId}", method = RequestMethod.POST)
    public Result closeChat(@PathVariable Long courseId) {
        return courseService.closeChat(courseId);
    }


    @RequestMapping(value = "/checkIn/students/{courseId}", method = RequestMethod.GET)
    public Result getAllSignedStudents(@PathVariable Long courseId) {
        return courseService.getSignedStudents(courseId);
    }

    @RequestMapping(value = "/course/upload/question/{courseId}", method = RequestMethod.POST)
    public Result uploadQuestion(@PathVariable Long courseId, Question question) {
        if(question == null)
            return getFailureDetail("question 内容为空");
        question.setCourseId(courseId);
        return questionService.uploadQuestion(question);
    }

    @RequestMapping(value = "/course/delete/question/{questionId}", method = RequestMethod.POST)
    public Result deleteQuestion(@PathVariable Long questionId) {
        return questionService.deleteQuestion(questionId);
    }

    @RequestMapping(value = "/question/{courseId}", method = RequestMethod.GET)
    public Result getQuestionToday(@PathVariable Long courseId) {
        return questionService.getQuestionByTeacher(courseId);
    }



}
